﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace ALDIOrganizer
{
    [Serializable]
    [DebuggerDisplay("{open} - {close}")]
    public struct WorkingHoursSpan : ICloneable, IComparable
    {
        public TimeByQuarters Open;
        public TimeByQuarters Close;

        #region ICloneable Members
        public object Clone()
        {
            return Cloner.Copy<WorkingHoursSpan>(this);
        }
        #endregion

        /// <summary>
        /// returns GetString Value
        /// </summary>
        /// <remarks>
        /// needed for data binding only
        /// (since binding to the ToString() method directly is a no-no afaik)
        /// </remarks>
        public string GetToString
        {
            get
            {
                return this.ToString();
            }
        }


        /* Return the values contained in this structure
         * in a "civilized" string pFormat, such as 8 - 20:30 */
        public override string ToString()
        {
            string _result = this.Open.GetHour.ToString();
            if (this.Open.GetMinutes != 0)
            {
                _result += ":" + this.Open.GetMinutes.ToString();
            }

            _result += " - " + this.Close.GetHour.ToString();
            if (this.Close.GetMinutes != 0)
            {
                _result += ":" + this.Close.GetMinutes.ToString();
            }
            return _result;
        }

        /// <remarks>
        /// This method is used by SheetView's RowComparer nested class.
        /// When editing a template, with every row insertion the rows get sorted automatically.
        /// The default order is from manager down to store assistant.
        /// The problem occured among rows representing shifts for employees of the same job position.
        /// You'd add a row for store assistant, filled with (say) 8 - 14, 8 - 14, 8 - 14,
        /// then another one, filled with 14 - cl, 14 - cl, 14 - cl.
        /// After sorting, the "afternoon row" could land ABOVE the morning row.
        /// To prevent this, SheetView's averageTime method calculates the AVERAGE hours for all the
        /// shifts in a row (if there is 8 - 15 and 10 - 17:30, average time is 9 - 16:15)
        /// so that the "morning" rows can be put above the "evening" ones.
        /// And how is the program supposed to know whether 8 - 10 is "smaller" than 9 - 11?
        /// This very CompareTo implementation tells is that.
        /// Opening hour time has got priority - if it's of the same value, closing hour time is
        /// taken into account (so that 10 - 19 goes AFTER 10 - 16).
        /// 
        /// One exception is an empty row. When it contains no shifts at all, the average time is
        /// 0 - 0 and therefore an empty row (freshly added row) would always end up at the top of the
        /// whole bunch. That's why the method temporarily substitutes 0 for 100 when comparing.
        /// 100 is actually a non-existent hour (100 * 15 minutes = 25 hours), kinda like in that
        /// Spike Lee film. This guarantees that an empty row is seen as "later" and therefore
        /// put below any row that's got some shifts in it. 
        /// </remarks>
        public int CompareTo(object obj)
        {
            var other = (WorkingHoursSpan) obj;
            var thisOpen = (Open.Time == 0) ? 100 : Open.Time;
            var thisClose = (Close.Time == 0) ? 100 : Close.Time;
            var otherOpen = (other.Open.Time == 0) ? 100 : other.Open.Time;
            var otherClose = (other.Close.Time == 0) ? 100 : other.Close.Time;
            return thisOpen == otherOpen ?
                thisClose.CompareTo(otherClose) :
                thisOpen.CompareTo(otherOpen);
        }

        public static string[] ToStringArray(WorkingHoursSpan[] pWorkingHoursArray)
        {
            string[] _row = new string[pWorkingHoursArray.Length + 1];
            for (int i = 0; i < pWorkingHoursArray.Length; i++)
            {
                _row[i + 1] = pWorkingHoursArray[i].ToString();
            }
            return _row;
        }
    }
}
